package cn.k.controller;

import cn.k.domain.*;
import cn.k.domain.Consumption;
import cn.k.qo.ConsumptionQueryObject;
import cn.k.qo.JSONResult;
import cn.k.service.*;
import cn.k.service.IConsumptionService;
import cn.k.util.RequirePermission;
import cn.k.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "结算查询", expression = "consumption:list")
    public String list(@ModelAttribute("qo") ConsumptionQueryObject qo, Model model) {
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询结算所有数据封装到Model中
        model.addAttribute("consumptions", consumptionService.selectAll());
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);

        return "consumption/list";
    }


    //处理删除请求
    @RequestMapping("/delete")
    @RequirePermission(name = "结算删除", expression = "consumption:delete")
    public String delete(Long id) {
        if (id != null) {
            consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }

    //处理添加或者更新的请求,跳转页面
    @RequestMapping("/input")
    @RequirePermission(name = "结算编辑", expression = "consumption:saveOrUpdate")
    public String input(Long id, Model model) {
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);
        //修改,带id值
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption", consumption);

            //查询结算明细表
            List<ConsumptionItem> consumptionItems=consumptionItemService.queryByCno(consumption.getCno());
            model.addAttribute("consumptionItems",consumptionItems);

            //业务二级联动,结算类型
            List<SystemDictionary> category = systemDictionaryService.queryByService("business");
            List<SystemDictionary> payType = systemDictionaryService.queryByService("settlement_type");
            model.addAttribute("category",category);
            model.addAttribute("payType",payType);




        }
        return "consumption/input";
    }

    //处理添加或者更新的请求
    @RequirePermission(name ="结算编辑(无预约单)",expression = "consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption){
        //判断有没有id
        if (consumption.getId()==null){
            consumptionService.insert(consumption);
        }else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }

    @RequirePermission(name ="结算编辑(有预约单)",expression = "consumption:save")
    @RequestMapping("/save")
    //接收的是预约单的id
    public String save(Long aid){
        //预约单状态:消费中状态
        //新增预约的关联结算中
        Long cid=consumptionService.save(aid);
        return "redirect:/consumption/input?id="+cid;
    }

    //更新状态
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id, int status){
        Consumption consumption = consumptionService.get(id);
         if (status==Consumption.STATUS_TYPE_AUDIT){
             consumption.setPayTime(new Date());
             consumption.setPayee(UserContext.getEmployee());
         }else if (status==Consumption.STATUS_TYPE_FINISH){
             consumption.setAuditTime(new Date());
             consumption.setAuditor(UserContext.getEmployee());
         }
         consumptionService.update(consumption);
        consumptionService.updateStatus(id,status);
        return "redirect:/consumption/input?id="+id;

    }

    



}
